import Vue from 'vue'
// 白名单
const whiteList = [
	'/pages/tabBarIndex/homeIndex',
	'/pages/tabBarIndex/ownsIndex',
	'/pages/public/noNetWork'
]

export default async function() {
	const list = ['navigateTo', 'redirectTo', 'reLaunch', 'switchTab']
	list.forEach(item => {
		uni.addInterceptor(item, {
			invoke(e) {
				let url = e.url.split('?')[0]
				// H5访问首页路径为'/' 非H5为HomeIndex完整路径
				if (url == '/') {
					url = '/pages/tabBarIndex/homeIndex'
				}
				let pass
				if (whiteList) {
					pass = whiteList.some((item) => {
						if (typeof(item) === 'object' && item.pattern) {
							return item.pattern.test(url)
						}
						return url === item
					})
				}
				// 不是白名单且没登录
				if (!pass && !uni.getStorageSync('accessToken')) {
					let pages = getCurrentPages();
					let currentPage = pages[pages.length - 1]
					return false
				}
				return e
			},
		})
	})
}